package com.sss.otep.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sss.otep.client.grid.MemberDataModel;
import com.sss.otep.client.grid.MemberPaidDeptDataModel;
import com.sss.otep.message.schemas.ObjectFactory;
import com.sss.otep.orm.model.Member;
import com.sss.otep.orm.model.MemberPaidDept;


public abstract class SchemaConvertionUtils {
	private static ObjectFactory schemaTypeFactory = new ObjectFactory();
	private static final Logger log = LoggerFactory.getLogger(SchemaConvertionUtils.class);
	/**
	 * convert java.util.Date to XMLGregorianCalendar
	 * @param java.util.Date 
	 * @return
	 */
	private static XMLGregorianCalendar toXMLGregorianCalendar(Date d){
		Calendar c = new GregorianCalendar(); 
		c.setTime(d);
		int day = c.get(Calendar.DAY_OF_MONTH);
		int month = c.get(Calendar.MONTH) +1;
		int year = c.get(Calendar.YEAR);
		int timezone = c.getTimeZone().getOffset(d.getTime())/(60*60*1000);
		try {
			return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(year, month, day, timezone);
		} catch (DatatypeConfigurationException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}
	private static Date toDate(XMLGregorianCalendar d){		
		return d.toGregorianCalendar().getTime();
	}	
	
	public static com.sss.otep.message.schemas.Member toSchema(Member member){
		com.sss.otep.message.schemas.Member m = schemaTypeFactory.createMember();
		m.setCpkId(member.getCpkId());
		m.setCpkMemAmphur(member.getCpkMemAmphur());
		m.setCpkMemDepartment(member.getCpkMemDepartment());
		m.setCpkMemIdcard(member.getCpkMemIdcard());
		m.setCpkMemName(member.getCpkMemName());
		m.setCpkMemOrgname(member.getCpkMemOrgname());
		m.setCpkMemProvince(member.getCpkMemProvince());
		m.setCpkMemStatus(member.getCpkMemStatus());
		m.setCpkMemSurname(member.getCpkMemSurname());
		return m;
	}
	public static com.sss.otep.message.schemas.MemberPaidDept toSchema(MemberPaidDept memberPaidDept){
		com.sss.otep.message.schemas.MemberPaidDept m = schemaTypeFactory.createMemberPaidDept();
		m.setCpkDueDtl(memberPaidDept.getCpkDueDtl());
		m.setCpkId(memberPaidDept.getCpkId());
		m.setCpkMemDepartment(memberPaidDept.getCpkMemDepartment());
		m.setCpkMemIdcard(memberPaidDept.getCpkMemIdcard());
		m.setCpkMemName(memberPaidDept.getCpkMemName());
		m.setCpkMemOrgName(memberPaidDept.getCpkMemOrgName());
		m.setCpkMemSurname(memberPaidDept.getCpkMemSurname());
		m.setCpkPaidATM(memberPaidDept.getCpkPaidATM());
		m.setCpkPaidDate(toXMLGregorianCalendar(memberPaidDept.getCpkPaidDate()));
		m.setCpkType(memberPaidDept.getCpkType());
		return m;
	}
	public static Member toModel(com.sss.otep.message.schemas.Member member){
		Member m = new Member();
		m.setCpkId(member.getCpkId());
		m.setCpkMemAmphur(member.getCpkMemAmphur());
		m.setCpkMemDepartment(member.getCpkMemDepartment());
		m.setCpkMemIdcard(member.getCpkMemIdcard());
		m.setCpkMemName(member.getCpkMemName());
		m.setCpkMemOrgname(member.getCpkMemOrgname());
		m.setCpkMemProvince(member.getCpkMemProvince());
		m.setCpkMemStatus(member.getCpkMemStatus());
		m.setCpkMemSurname(member.getCpkMemSurname());
		m.setCpkType(member.getCpkType());		
		return m;
	}
	public static MemberDataModel toGridModel(Integer total,com.sss.otep.message.schemas.Member member){		
		return new MemberDataModel(total+1,member);
	}
	public static MemberPaidDeptDataModel toGridModel(Integer total,com.sss.otep.message.schemas.MemberPaidDept mpd){		
		return new MemberPaidDeptDataModel(total+1,mpd);
	}
	public static MemberPaidDept toModel(com.sss.otep.message.schemas.MemberPaidDept memberPaidDept){
		MemberPaidDept mpd = new MemberPaidDept();
		mpd.setCpkDueDtl(memberPaidDept.getCpkDueDtl());
		mpd.setCpkId(memberPaidDept.getCpkId());
		mpd.setCpkMemDepartment(memberPaidDept.getCpkMemDepartment());
		mpd.setCpkMemIdcard(memberPaidDept.getCpkMemIdcard());
		mpd.setCpkMemName(memberPaidDept.getCpkMemName());
		mpd.setCpkMemOrgName(memberPaidDept.getCpkMemOrgName());
		mpd.setCpkMemSurname(memberPaidDept.getCpkMemSurname());
		mpd.setCpkPaidATM(memberPaidDept.getCpkPaidATM());
		mpd.setCpkPaidDate(toDate(memberPaidDept.getCpkPaidDate()));
		mpd.setCpkType(memberPaidDept.getCpkType());
		return mpd;
	}
	
}
